package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;

import java.util.ArrayList;
import java.util.List;

public class VisitorActivity extends BaseActivity implements View.OnClickListener {

    //private RecyclerView recyclerView;
    private List<ApplyMessagebean.CustomInfoEntity> list;
    //   private View footView;
    private SwipeMenuListView my_listView;
    private MyAdapter adapter;

    @Override
    protected void onResume() {
        if (adapter != null)
            adapter.notifyDataSetChanged();
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);
        initTitle();
        list = ApplyMessageActivity.bean.getCustom_info();
        initView();


//        for (int i = 0; i < 3; i++) {
//            VisitorBean bean = new VisitorBean();
//            bean.setName(getResources().getString(R.string.visitor_item_name_tv));
//            bean.setPhone(getResources().getString(R.string.visitor_item_phone_tv));
//            bean.setPosition(getResources().getString(R.string.visitor_item_position_tv));
//            bean.setSex(getResources().getString(R.string.visitor_item_sex_tv));
//            bean.setGrade(getResources().getString(R.string.visitor_item_grade_tv));
//            list.add(bean);
//        }

        // footView = LayoutInflater.from(this).inflate(R.layout.includr_visitor_footview, null);
//        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.HORIZONTAL
//                , getResources().getDimensionPixelSize(R.dimen.line_h), getResources().getColor(R.color.line_color)));

//        adapter = new BaseRVAdapter<ApplyMessagebean.CustomInfoEntity>(this, list) {
//            @Override
//            public int getLayoutId(int viewType) {
//                return R.layout.item_visiton;
//            }
//
//            @Override
//            public void onBind(BaseViewHolder holder, final int position) {
//                holder.setTextView(R.id.tv_name, list.get(position).getCustomer());
//                holder.setTextView(R.id.tv_phone, mList.get(position).getTel());
//                holder.setTextView(R.id.tv_position, mList.get(position).getPosition());
//                String sex = "男";
//                if ("1".equals(mList.get(position).getSex())) {
//                    sex = "男";
//                } else if ("0".equals(mList.get(position).getSex())) {
//                    sex = "女";
//                } else {
//                    sex = "保密";
//                }
//                holder.setTextView(R.id.tv_sex, sex);
//                holder.setTextView(R.id.tv_grade, mList.get(position).getLevel_name());
//                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent intent = new Intent(VisitorActivity.this, AddPeoeleActivity.class);
//                        intent.putExtra("index", position);
//                        startActivity(intent);
//                    }
//                });
//            }
//        };
//        adapter.addFooterView(footView);
        //  recyclerView.setAdapter(adapter);
        if (ApplyMessageActivity.bean.getCanUpdata().equals("0")) {
            findViewById(R.id.add_visitor_lay).setVisibility(View.GONE);


        } else {
            findViewById(R.id.add_visitor_lay).setOnClickListener(this);
            findViewById(R.id.add_visitor_lay).setVisibility(View.VISIBLE);
        }
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private void initView() {
        my_listView = (SwipeMenuListView) findViewById(R.id.my_listView);
        adapter = new MyAdapter(list);
        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {


                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(VisitorActivity.this);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set item title
                deleteItem.setTitle("删除");
                // set item title fontsize
                deleteItem.setTitleSize(18);
                // set item title font color
                deleteItem.setTitleColor(Color.WHITE);
//                // set a icon
//                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };

        my_listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {

                    case 0:
                        //删除的逻辑
                        showDialogv7("确定删除该人员吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // updelete(position);
                                list.remove(position);
                                adapter.notifyDataSetChanged();

                            }
                        });
                        break;

                    default:
                        break;
                }
                return false;
            }
        });

        my_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(VisitorActivity.this, AddPeoeleActivity.class);
                intent.putExtra("index", position);
                startActivityForResult(intent, 3343);
            }
        });
        my_listView.setAdapter(adapter);

        if (ApplyMessageActivity.bean.getCanUpdata().equals("0")) {
            findViewById(R.id.add_visitor_lay).setVisibility(View.GONE);


        } else {
            findViewById(R.id.add_visitor_lay).setVisibility(View.VISIBLE);

            my_listView.setMenuCreator(creater);
        }

    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        MyToast(requestCode + "===adsasdasda===" + resultCode);
//        if (resultCode == RESULT_OK) {
//            switch (requestCode) {
//                case 3343:
//                    adapter.notifyDataSetChanged();
//
////                    initView();
//                    break;
//            }
//        }
//        super.onActivityResult(requestCode, resultCode, data);
//    }

    class MyAdapter extends BaseAdapter {
        List<ApplyMessagebean.CustomInfoEntity> list = new ArrayList<>();

        public MyAdapter(List<ApplyMessagebean.CustomInfoEntity> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHolder holder;
            if (convertView == null) {
                holder = new MyHolder();
                convertView = View.inflate(VisitorActivity.this, R.layout.item_visiton, null);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
                holder.tv_position = (TextView) convertView.findViewById(R.id.tv_position);
                holder.tv_sex = (TextView) convertView.findViewById(R.id.tv_sex);
                holder.tv_grade = (TextView) convertView.findViewById(R.id.tv_grade);
                convertView.setTag(holder);
            } else {
                holder = (MyHolder) convertView.getTag();
            }
            String sex = "男";
            if ("1".equals(list.get(position).getSex())) {
                sex = "男";
            } else if ("0".equals(list.get(position).getSex())) {
                sex = "女";
            } else {
                sex = "保密";
            }
            holder.tv_sex.setText(sex);
            holder.tv_name.setText(list.get(position).getCustomer());
            holder.tv_phone.setText(list.get(position).getTel());
            holder.tv_position.setText(list.get(position).getPosition());
            holder.tv_grade.setText(list.get(position).getLevel_name());

            return convertView;
        }

        class MyHolder {
            TextView tv_name, tv_phone, tv_position, tv_sex, tv_grade;
        }
    }

    private TextView title;

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText(R.string.visitor_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        submit.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            //新增来访人
            case R.id.add_visitor_lay:
                intent = new Intent(this, AddPeoeleActivity.class);
                startActivity(intent);
                break;
            case R.id.submit:

                setResult(RESULT_OK);
                finish();
                break;
        }
    }
}
